package tnviet.proj.jna.test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.text.MaskFormatter;
import tnviet.proj.jna.test.IPField;

public class IPField extends JFormattedTextField implements ActionListener, KeyListener {

	final String mask = "###.###.###.###";
	final String validChars = "0,1,2,3,4,5,6,7,8,9";
	final int focusLostBehavior = JFormattedTextField.PERSIST;
	final int colWidth = 12;
	final Dimension preferredSize = new Dimension(75, 20);
	final int tfAlign = SwingConstants.CENTER;
	final Font tfFont = new Font(null, Font.PLAIN, 14);
	final Border tfBorder = BorderFactory.createLineBorder(Color.black, 1);
	final String errorMsg = "Invaid Entry.  Please enter a valid entry.";
	final String btnTitle = "Show IP";
	private MaskFormatter mf1;

	public IPField() {//throws ParseException {
		//super();

		//MaskFormatter mf1 = new MaskFormatter(mask);
		createMaskFormatter();
		mf1.setValidCharacters(validChars);
		setFormatter(mf1);
		setFocusLostBehavior(focusLostBehavior);
		setColumns(colWidth);
		setPreferredSize(preferredSize);
		setHorizontalAlignment(tfAlign);
		setFont(tfFont);
		setBorder(tfBorder);
		addKeyListener(new KeyAdapter() {
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (c == '.') {
					int newCaretPos = getNewCaretPos();
					if (newCaretPos != 999)
						moveCaret(newCaretPos);
				}
			}
		});

		//KeyBoardFocusManager is used to monitor when TAB is pressed
		KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
			public boolean dispatchKeyEvent(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_TAB) {
					int newCaretPos = getNewCaretPos();
					if (newCaretPos != 999) {
						moveCaret(newCaretPos);
					}
					return true;
				} else {
					return false;
				}
			}
		});
	}

	private void createMaskFormatter() {
		try {
			mf1 = new MaskFormatter(mask);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	public int getNewCaretPos() {
		int curCaretPos = getCaretPosition();
		switch (curCaretPos) {
		case 0 :
			return 0;
		case 1 :
		case 2 :
			return 4;
		case 5 :
		case 6 :
			return 8;
		case 9 :
		case 10 :
			return 12;
		default :
			return 999;
		}
	}

	public void moveCaret(int newPos) {
		setCaretPosition(newPos);
	}

	public void actionPerformed(ActionEvent e) {
		getText().replaceAll(" ", "");
	}

	public void keyTyped(KeyEvent e) {
	}
	public void keyPressed(KeyEvent e) {
	}
	public void keyReleased(KeyEvent e) {
	}

        public static void main(String[] args){
            JFrame frame = new JFrame();
            IPField filed = new IPField();
            frame.getContentPane().add(filed);
            frame.pack();
            frame.setVisible(true);
        }
}
